// pages/myOrder/myOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0,  //切换订单状态
    orderListLeg: 0,              //订单数量
    orderList: [],                      //订单列表
  },
  /**
   * 获取不同状态的订单列表
   */
  getStatusList: function (status) {
    let that = this;
    wx.showLoading({
      title: '加载中',
    });
    let statusArr = [];
    let newStatus = that.data.status;
    if (newStatus == 0) {
      statusArr = [1, 2, 3, 4, 5, 6, -1]
    } else if (newStatus == 1) {
      statusArr = [1]
    } else if (newStatus == 3) {
      statusArr = [2, 3, 4]
    } else {
      statusArr = [5]
    }
    console.log(statusArr)
    console.log("zli")
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/order/findByUserIdAndStatus',
      method: 'post',
      data: {
        userId: wx.getStorageSync("userId"),
        statusList: statusArr,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          orderList: res.data.data,
          orderListLeg: res.data.data.length,
        })
        console.log(that.data.orderListLeg);
        // setTimeout(function () {
          wx.hideLoading()
      }
        // }, 1000)      }
    })
    console.log(statusArr)
  },

  /**
   * 改变头部状态
   */
  changeStatus: function (e) {
    console.log(e)
    this.setData({
      status: e.currentTarget.dataset.status
    })
    this.getStatusList(this.data.status);
  },

  /**
   * 进入订单详情界面
   */
  goOrderDetails: function (e) {
    let orderId = e.currentTarget.dataset.orderid;
    console.log(orderId);
    wx.navigateTo({
      url: '../orderDetails/orderDetails?orderId=' + orderId,
    })
  },
  
  buy: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    var fee = e.currentTarget.dataset.fee;
    console.log(fee);
    var total=parseFloat(fee)*100;
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/order/pay',
      method: "post",
      data: {
        orderId: orderId,
        totalFee: total
      },
      success: function (res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          "appId": res.data.appId,
          'paySign': res.data.paySign,
          success: function (res) {
            console.log('success');
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 3000
            });

            wx.showToast({
              title: '商品购买成功',
              success: function () {
                setTimeout(function () {

                  wx.redirectTo({
                    url: '../orderDetails/orderDetails?orderId' + that.data.orderId,
                  });
                }, 2000)
              }
            })

            wx.request({
              url: 'https://www.xiaozheng8.com/shop/order/setStatus',
              method: "post",
              data: {
                orderId:orderId,
                status: 2
              }
            })
          }, fail: function (res) {
            console.log(res);
            console.log("取消支付");
            // ({
            //   url: '../shopping/shopping?cateId' + that.data.productId,
            // });
            wx.showToast({
              title: '支付已取消',
              icon: "none"
            });
            wx.request({
              url: 'https://www.xiaozheng8.com/shop/order/setStatus',
              method: "post",
              data: {
                orderId: orderId,
                status: 1
              },

            })
          }
        })

      }

    })
  },
  goDifferentPage: function (options) {
    this.setData({
      status: options.status
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**
          * 进入不同订单状态页面
          */
    wx.showLoading({
      title: '加载中',
    })
    this.goDifferentPage(options);
    this.getStatusList(options);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getStatusList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})